package com.example.gesture.PointerInput

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.PointerInputChange
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import kotlin.math.roundToInt

//拖动手势API
//detectDragGestures	监听拖动手势
//detectDragGesturesAfterLongPress	监听长按后的拖动手势
//detectHorizontalDragGestures	监听水平拖动手势
//detectVerticalDragGestures	监听垂直拖动手势

//Lamda表达式
//这里提供了四个回调时机，onDragStart 会在拖动开始时回调，onDragEnd 会在拖动结束时回调，onDragCancel 会在拖动取消时回调，而 onDrag 则会在拖动真正发生时回调。

@Composable
fun DragGestureDemo() {
    val boxSize = 100.dp
    var offset by remember {
        mutableStateOf(Offset.Zero)
    }

    Box(
        modifier = Modifier
            .size(boxSize)
            .offset { IntOffset(offset.x.roundToInt(), offset.y.roundToInt()) }
            .background(Color.Green)
            .pointerInput(Unit) {
                detectDragGestures(
                    onDragStart = { Log.d("xu", "拖动开始了") },
                    onDragEnd = { Log.d("xu", "拖动结束了") },
                    onDragCancel = { Log.d("xu", "拖动取消了") },
                    onDrag = { change: PointerInputChange, dragAmount: Offset ->
                        Log.d("xu" , "拖动中~")
                        offset += dragAmount
                    }
                )
            }
    )
}